import store from '@/store'
import tinymce from 'tinymce'
import { fontsizeMapBase } from '@/constants/fontsizemap'

export default function () {
  let extStyle = ``
  store.state.xmleditor.ruleFonts?.forEach((item) => {
    const fontSizeZh = fontsizeMapBase[item.fontSizeZh]
    const fontSizeEn = fontsizeMapBase[item.fontSizeEn]
    const familyZh = item.fontFamilyZh
    const familyEn = item.fontFamilyEn
    const fontBoldZh = item.fontBoldZh === '1' ? 'bold' : 'normal'
    const fontBoldEn = item.fontBoldEn === '1' ? 'bold' : 'normal'
    const isvalid = (i) => i && i !== '-'
    /** type: undefined, 'en', 'zh' */
    const setStyleBySelector = (sel, type) => {
      const sizezh = isvalid(fontSizeZh) ? `font-size: ${fontSizeZh};` : ''
      const sizeen = isvalid(fontSizeEn) ? `font-size: ${fontSizeEn};` : ''
      const family = familyEn === familyZh ? `font-family: ${familyEn};` : `font-family: ${familyEn}, ${familyZh};`
      const familyzh = isvalid(familyZh) ? `font-family: ${familyZh};` : ''
      const familyen = isvalid(familyEn) ? `font-family: ${familyEn};` : ''
      const boldzh = isvalid(fontBoldZh) ? `font-weight: ${fontBoldZh};` : ''
      const bolden = isvalid(fontBoldEn) ? `font-weight: ${fontBoldEn};` : ''
      if (type === 'en') {
        extStyle += ` ${sel} {${sizeen}${familyen}${bolden}}`
      } else if (type === 'zh') {
        extStyle += ` ${sel} {${sizezh}${familyzh}${boldzh}}`
      } else {
        extStyle += ` ${sel} {${sizezh}${family}${boldzh}}`
      }
    }
    if (item.sectionType === 'file_no_and_version') {
      // 文件编号及版本号
      setStyleBySelector('.page-sheet.cover .file_no_version')
      // setStyleBySelector('.page-sheet.cover .file_no_version', 'en')
    } else if (item.sectionType === 'company_logo') {
      // 公司 LOGO
    } else if (item.sectionType === 'secret_classify') {
      // 密级
      setStyleBySelector('.page-sheet.cover .secret_classify')
      // setStyleBySelector('.page-sheet.cover .secret_classify', 'zh')
      // setStyleBySelector('.page-sheet.cover .secret_classify_en', 'en')
    } else if (item.sectionType === 'file_type') {
      // 中国商用飞机有限责任公司 XX
      setStyleBySelector('.page-sheet.cover .file_type_zh', 'zh')
      setStyleBySelector('.page-sheet.cover .file_type_en', 'en')
    } else if (item.sectionType === 'file_name_zh') {
      // 文件名称
      setStyleBySelector('.page-sheet.cover .file_name_zh', 'zh')
    } else if (item.sectionType === 'file_name_en') {
      // 文件英文名称
      setStyleBySelector('.page-sheet.cover .file_name_en', 'en')
    } else if (item.sectionType === 'publish_date') {
      // 发布实施日期
      setStyleBySelector('.page-sheet.cover .publish_date', 'zh')
      setStyleBySelector('.page-sheet.cover .publish_date_en', 'en')
      setStyleBySelector('.page-sheet.cover .implement_date_en', 'en')
    } else if (item.sectionType === 'edit_unit') {
      // 编制单位名称
      setStyleBySelector('.page-sheet.cover .edit_unit')
      // setStyleBySelector('.page-sheet.cover .edit_unit_zh', 'zh')
      // setStyleBySelector('.page-sheet.cover .edit_unit_en', 'en')
    } else if (item.sectionType === 'statement_title') {
      // 专有信息声明
      setStyleBySelector('.page-sheet .page_footer p.footer-title')
      // setStyleBySelector('.page-sheet .page_footer p.footer-title', 'zh')
      // setStyleBySelector('.page-sheet .page_footer p.footer-title .stmt_tit_en', 'en')
    } else if (item.sectionType === 'statement_content') {
      //专有信息声明内容
      setStyleBySelector('.page-sheet .page_footer p.footer-body')
      // setStyleBySelector('.page-sheet .page_footer p.footer-body', 'zh')
      // setStyleBySelector('.page-sheet .page_footer p.footer-body .stmt_body_en', 'en')
    } else if (item.sectionType === 'signature_title_and_content') {
      // 签署栏及内容
      setStyleBySelector('.page-sheet.signature .row-list .row')
      // setStyleBySelector('.page-sheet.signature .row-list .row', 'zh')
      // setStyleBySelector('.page-sheet.signature .row-list .row span', 'en')
    } else if (item.sectionType === 'signature_counter_title') {
      // 会签栏
      setStyleBySelector('.page-sheet.signature table > tbody > tr:nth-child(1) > td')
      // setStyleBySelector('.page-sheet.signature table > tbody > tr:nth-child(1) > td', 'zh')
      // setStyleBySelector('.page-sheet.signature table > tbody > tr:nth-child(1) > td span', 'en')
    } else if (item.sectionType === 'signature_counter_content') {
      // 会签内容
      setStyleBySelector('.page-sheet.signature table td')
      // setStyleBySelector('.page-sheet.signature table td', 'zh')
      // setStyleBySelector('.page-sheet.signature table td span', 'en')
    } else if (item.sectionType === 'revision_title') {
      // 更改记录字样
      setStyleBySelector('.page-sheet.revision .title_zh')
      // setStyleBySelector('.page-sheet.revision .title_zh', 'zh')
      // setStyleBySelector('.page-sheet.revision .title_zh span', 'en')
    } else if (item.sectionType === 'revision_content') {
      // 更改记录内容
      setStyleBySelector('.page-sheet.revision table')
      // setStyleBySelector('.page-sheet.revision table', 'zh')
      // setStyleBySelector('.page-sheet.revision table span', 'en')
    } else if (item.sectionType === 'contents_title') {
      // 目次
      setStyleBySelector('.page-sheet.contents .page-body .title.title_zh', 'zh')
      setStyleBySelector('.page-sheet.contents .page-body .title', 'zh')
      setStyleBySelector('.page-sheet.contents .page-body .title.title_en', 'en')
    } else if (item.sectionType === 'contents_content') {
      // 目次内容
      setStyleBySelector('.page-sheet.contents .page-body .row')
      // setStyleBySelector('.page-sheet.contents .page-body .row', 'zh')
      // setStyleBySelector('.page-sheet.contents .page-body .row .chapter-title-en', 'en')
    } else if (item.sectionType === 'foreword_title') {
      // 前言
      setStyleBySelector('.page-sheet.foreword .page-body p.title.title_zh', 'zh')
      setStyleBySelector('.page-sheet.foreword .page-body p.title', 'zh')
      setStyleBySelector('.page-sheet.foreword .page-body p.title.title_en', 'en')
    } else if (item.sectionType === 'foreword_content') {
      // 前言内容
      setStyleBySelector('.page-sheet.foreword .page-body')
      setStyleBySelector('.page-sheet.foreword p[data-type="paragraph"]')
      // setStyleBySelector('.page-sheet.foreword p[data-type="paragraph"]', 'zh')
      // setStyleBySelector('.page-sheet.foreword p[data-type="paragraph"][data-lang="en"]', 'en')
    } else if (item.sectionType === 'introduction_title') {
      // 引言
      setStyleBySelector('.page-sheet.introduction .page-body p.title.title_zh', 'zh')
      setStyleBySelector('.page-sheet.introduction .page-body p.title', 'zh')
      setStyleBySelector('.page-sheet.introduction .page-body p.title.title_en', 'en')
    } else if (item.sectionType === 'introduction_content') {
      // 引言内容
      setStyleBySelector('.page-sheet.introduction .page-body')
      setStyleBySelector('.page-sheet.introduction p[data-type="paragraph"]')
      // setStyleBySelector('.page-sheet.introduction p[data-type="paragraph"]', 'zh')
      // setStyleBySelector('.page-sheet.introduction p[data-type="paragraph"][data-lang="en"]', 'en')
    } else if (item.sectionType === 'paragraph') {
      setStyleBySelector('.page-sheet p[data-type="paragraph"]')
      // setStyleBySelector('.page-sheet p[data-type="paragraph"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="paragraph"][data-lang="en"]', 'en')
    } else if (item.sectionType === 'chapter') {
      // 章编号和章标题
      setStyleBySelector('.page-sheet p[data-type="scope"]')
      setStyleBySelector('.page-sheet p[data-type="design_basis"]')
      setStyleBySelector('.page-sheet p[data-type="normative_references"]')
      setStyleBySelector('.page-sheet p[data-type="terms_definitions"]')
      setStyleBySelector('.page-sheet p[data-type="symbols_abbreviation"]')
      setStyleBySelector('.page-sheet p[data-type="chapter"]')
      // setStyleBySelector('.page-sheet p[data-type="scope"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="design_basis"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="normative_references"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="terms_definitions"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="symbols_abbreviation"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="chapter"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type="scope"] [data-content-type="chapter_title_en"]', 'en')
      // setStyleBySelector('.page-sheet p[data-type="design_basis"] [data-content-type="chapter_title_en"]', 'en')
      // setStyleBySelector('.page-sheet p[data-type="normative_references"] [data-content-type="chapter_title_en"]', 'en')
      // setStyleBySelector('.page-sheet p[data-type="terms_definitions"] [data-content-type="chapter_title_en"]', 'en')
      // setStyleBySelector('.page-sheet p[data-type="symbols_abbreviation"] [data-content-type="chapter_title_en"]', 'en')
      // setStyleBySelector('.page-sheet p[data-type="chapter"] [data-content-type="chapter_title_en"]', 'en')
      // setStyleBySelector('.page-sheet.annex p[data-type="item1"].list-title', 'zh')
      // setStyleBySelector('.page-sheet.annex p[data-type="item1"] [data-content-type="chapter_title_en"]', 'en')
    } else if (item.sectionType === 'item') {
      // 条编号、标题
      setStyleBySelector('.page-sheet p[data-type^="item"]')
      // setStyleBySelector('.page-sheet p[data-type^="item"]', 'zh')
      // setStyleBySelector('.page-sheet p[data-type^="item"] [data-content-type="chapter_title_en"]', 'en')
    } else if (item.sectionType === 'item_list') {
      // 列项编号、内容
      setStyleBySelector('.page-sheet p.item_list.zh', 'zh')
      setStyleBySelector('.page-sheet p.item_list.en', 'en')
    } else if (item.sectionType === 'example') {
      // 示例
      setStyleBySelector('.page-sheet .page_body p.example')
      setStyleBySelector('.page-sheet .page-body p.example')
      setStyleBySelector('.page-sheet .page_body p.item_list[data-is-example]')
      setStyleBySelector('.page-sheet .page-body p.item_list[data-is-example]')
      setStyleBySelector('.page-sheet .page-body p[data-type="paragraph"][data-is-example]')
      setStyleBySelector('.page-sheet .page_body p[data-type="paragraph"][data-is-example]')

      //setStyleBySelector('.page-sheet p.example.zh', 'zh')
      //setStyleBySelector('.page-sheet p.item_list.zh[data-is-example]', 'zh')
      //setStyleBySelector('.page-sheet p[data-type="paragraph"][data-is-example]:not([data-lang])', 'zh')
      //setStyleBySelector('.page-sheet p[data-type="paragraph"][data-is-example][data-lang="zh"]', 'zh')
      //setStyleBySelector('.page-sheet p.example.en', 'en')
      //setStyleBySelector('.page-sheet p.item_list.en[data-is-example]', 'en')
      //setStyleBySelector('.page-sheet p[data-type="paragraph"][data-is-example][data-lang="en"]', 'en')
    } else if (item.sectionType === 'item_note') {
      // 条文中的注
      setStyleBySelector('.page-sheet .page_body p.item_note')
      setStyleBySelector('.page-sheet .page-body p.item_note')
      //setStyleBySelector('.page-sheet p.item_note.zh', 'zh')
      //setStyleBySelector('.page-sheet p.item_note.en', 'en')
    } else if (item.sectionType === 'table_title') {
      // 表的编号、标题、续表的\"（续）\"字
      setStyleBySelector(
        '.page-sheet .page-body table.data-content-table caption.zh .caption, .page-sheet .page_body table.data-content-table caption.zh .caption',
        'zh'
      )
      setStyleBySelector(
        '.page-sheet .page-body table.data-content-table caption.en .caption, .page-sheet .page-body table.data-content-table caption.en .caption',
        'en'
      )
    } else if (item.sectionType === 'table_content') {
      // 表中的文字、表注和脚注
      setStyleBySelector(
        '.page-sheet .page-body table.data-content-table[data-type="table"] th p, .page-sheet .page_body table.data-content-table[data-type="table"] th p, .page-sheet .page-body table.data-content-table[data-type="table"] td p, .page-sheet .page_body table.data-content-table[data-type="table"] td p'
      )
      // setStyleBySelector('.page-sheet .page-body table.data-content-table[data-type="table"] p', 'zh')
      // setStyleBySelector('.page-sheet .page-body table.data-content-table[data-type="table"] p[data-lang="en"]', 'en')
    } else if (item.sectionType === 'table_unit_desc') {
      // 表右上方关于单位的陈述
      setStyleBySelector('.page-sheet table.data-content-table caption.zh .unit', 'zh')
      setStyleBySelector('.page-sheet table.data-content-table caption.en .unit', 'en')
    } else if (item.sectionType === 'image_title') {
      // 图的编号、标题、续图的\"（续）\"字
      setStyleBySelector('.page-sheet .data-content-image-wrapper .data-content-image-caption.zh', 'zh')
      setStyleBySelector('.page-sheet .data-content-image-wrapper .data-content-image-caption.en', 'en')
    } else if (item.sectionType === 'figure_note') {
      // 图中的文字、图注及脚注
      setStyleBySelector('.page-sheet .data-content-image-wrapper p[data-type].zh', 'zh')
      setStyleBySelector('.page-sheet .data-content-image-wrapper p[data-type].en', 'en')
    } else if (item.sectionType === 'annex_number') {
      // 附录编号
      setStyleBySelector('.page-sheet.annex .page-body .annex-number', 'zh')
      setStyleBySelector('.page-sheet.annex .page-body .annex-number[data-lang="en"]', 'en')
    } else if (item.sectionType === 'annex_type') {
      // 附录性质
      setStyleBySelector('.page-sheet.annex .page-body .annex-type', 'zh')
      setStyleBySelector('.page-sheet.annex .page-body .annex-type[data-lang="en"]', 'en')
    } else if (item.sectionType === 'annex_name') {
      // 附录名称
      setStyleBySelector('.page-sheet.annex .page-body .annex-title', 'zh')
      setStyleBySelector('.page-sheet.annex .page-body .annex-title[data-lang="en"]', 'en')
      // } else if (item.sectionType === 'annex_chapter') {
      //   // 附录-章编号和章标题
      //   setStyleBySelector('.page-sheet.annex p[data-type="item1"].list-title', 'zh')
      //   setStyleBySelector('.page-sheet.annex p[data-type="item1"] [data-content-type="chapter_title_en"]', 'en')
      // } else if (item.sectionType === 'annex_item') {
      //   // 附录-条编号、标题
      //   setStyleBySelector('.page-sheet.annex p[data-type^="item"]', 'zh')
      //   setStyleBySelector('.page-sheet.annex p[data-type^="item"] [data-content-type="chapter_title_en"]', 'en')
      // } else if (item.sectionType === 'annex_item_list') {
      //   // 附录-列项编号、内容
      //   setStyleBySelector('.page-sheet.annex p.item_list.zh', 'zh')
      //   setStyleBySelector('.page-sheet.annex p.item_list.en', 'en')
      //   setStyleBySelector('.page-sheet.annex .page-body p[data-type="paragraph"]', 'zh')
      //   setStyleBySelector('.page-sheet.annex .page-body p[data-type="paragraph"][data-lang="en"]', 'en')
      // } else if (item.sectionType === 'annex_item_note') {
      //   // 附录-条文中的注
      //   setStyleBySelector('.page-sheet.annex p.item_note.zh', 'zh')
      //   setStyleBySelector('.page-sheet.annex p.item_note.en', 'en')
    } else if (item.sectionType === 'reference_title') {
      // 参考文献
      setStyleBySelector('.page-sheet.reference .page-body p.title.title_zh', 'zh')
      setStyleBySelector('.page-sheet.reference .page-body p.title', 'zh')
      setStyleBySelector('.page-sheet.reference .page-body p.title.title_en', 'en')
    } else if (item.sectionType === 'reference_content') {
      // 参考文献编号及内容
      setStyleBySelector('.page-sheet.reference .page-body')
      setStyleBySelector('.page-sheet.reference .page-body p[data-type="paragraph"]')
      // setStyleBySelector('.page-sheet.reference .page-body p[data-type="paragraph"]', 'zh')
      // setStyleBySelector('.page-sheet.reference .page-body p[data-type="paragraph"][data-lang="en"]', 'en')
    } else if (item.sectionType === 'index_title') {
      // 索引
      setStyleBySelector('.page-sheet.index .page-body p.title.title_zh', 'zh')
      setStyleBySelector('.page-sheet.index .page-body p.title', 'zh')
      setStyleBySelector('.page-sheet.index .page-body p.title.title_en', 'en')
    } else if (item.sectionType === 'index_content') {
      // 索引内容
      setStyleBySelector('.page-sheet.index .page-body')
      setStyleBySelector('.page-sheet.index .page-body p[data-type="paragraph"]')
      // setStyleBySelector('.page-sheet.index .page-body p[data-type="paragraph"]', 'zh')
      // setStyleBySelector('.page-sheet.index .page-body p[data-type="paragraph"][data-lang="en"]', 'en')
    } else if (item.sectionType === 'page_header_logo') {
      // 公司 LOGO
    } else if (item.sectionType === 'page_header_center') {
      // 文件编号和版本号
      setStyleBySelector('.page-sheet .page_header .file_no_version')
      // setStyleBySelector('.page-sheet .page_header .file_no_version', 'en')
    } else if (item.sectionType === 'page_header_no') {
      // 第 X 页 共 X 页
      setStyleBySelector('.page-sheet .page_header .page-number', 'zh')
      setStyleBySelector('.page-sheet .page_header.en .page-number', 'en')
    }
  })
  const doc = tinymce.activeEditor.getDoc()
  const styleNode = doc.createElement('style')
  styleNode.innerHTML = extStyle
  doc.head.appendChild(styleNode)
}
